import React, { Component } from 'react'
import { Button, Table, Modal, Row, Col, Popconfirm, Input, Form, message } from 'antd';
import { connect } from 'react-redux';
import { actionCreators } from '../categoryManage/store';
import { findUnpublished, getAllMetaDataTypes, getResourceInfo, getResourceTableInfo, publishResource} from '../../../api/resourceCategory/resourceCategory'
import CommonView from '../commonView'
import EditResource from '../categoryManage/editResource'
class UnAudit extends Component {
  constructor() {
    super()
    this.state = {
      dataSource: [],
      passDataSource: [],
      categoryNumber: '',
      resourceId: '',
      passBaseInfo: {},
      metedataTypes: [],
      publishVis: false,//发布显示
      editVis: false,//编辑
      paginationNo: {
        pageSize: 10,
        defaultCurrent:1,
        current:1,
      },
      columns : [
        {
          title: '操作',
          dataIndex: 'action',
          key: 'action',
          width: 220,
          render: (text, record) => {
            return (
              <div>
                <Button size="small" type="link" onClick={() => this.handlePublish(record)}>发布</Button>
                <Button size="small" type="link" onClick={() => this.handleEditClick(record)} disabled={record.exec_status === '已执行'}>编辑</Button>
                <Popconfirm
                  title="确认删除?"
                  onConfirm={() => this.handleDeleteClick(record.code)}
                  okText="确定"
                  cancelText="取消"
                >
                  <Button size="small" type="link" style={{ marginLeft: 10 }}>
                    删除
                    </Button>
                </Popconfirm>
              </div>
            )
          }
        }
      ]
    }


  }
  handleEditClick(record) {
    this.setState({
      categoryNumber: record.catalogue_number,
    })
    this.getInfo(record, 1)
  }

  handlePublish = record => {
    this.setState({resourceId: record.resource_id})
    this.getInfo(record, 0)
  }

  getInfo (record, type) { //type == 0 处理发布，type==1处理编辑
    let requestData = {resourceId: record.resource_id,}
    let passInfo = {}
    getResourceInfo(requestData).then(res => {
      if(res.data.metaData.code === 'success') {
        passInfo = res.data.data
        this.setState({passBaseInfo: res.data.data})
        this.props.setFormInitialData(res.data.data)
      }
    })
    getResourceTableInfo(requestData).then(res => {
      if(res.data.metaData.code === 'success') {
        type === 0 ? this.setState({passBaseInfo: passInfo, passDataSource: res.data.data,publishVis: true})
            :this.setState({passBaseInfo: passInfo,passDataSource: res.data.data, editVis: true})
      }
    })
    if(type === 1) {
      getAllMetaDataTypes({number: record.catalogue_number}).then(res => {
        if(res.data.metaData.code === 'success') {
          this.setState({metedataTypes: res.data.data})
        }
      })
    }
  }

  getUnPublished = (number, page, pageSize) => {
    findUnpublished({pageNum: page, pageSize: pageSize, number}).then(res => {
      if(res.data.metaData.code === 'success') {
        const paginationNo = this.state.paginationNo
        paginationNo.total = res.data.data.total
        paginationNo.current = res.data.data.pageNum
        this.setState({dataSource: res.data.data.list})
      }
    })
  }

  componentWillMount() {

      console.log('this.props.baseCOlumns', this.props.baseColumns)
      this.setState({
        columns: [...this.props.baseColumns ,...this.state.columns]
      })
      this.getUnPublished('01010101', 1,10)  
  }
  showModal = () => {
    this.setState({
      show: true
    })
  }
  handleOk = e => {
    this.setState({
      publishVis: false,
      editVis: false
    })
  }

  handleCancel = e => {
    this.setState({
      show: false
    })
  }

  handlePublishCate = () => {
    let obj ={resourceId: this.state.resourceId}
    publishResource(obj).then(res => {
      if(res.data.metaData.code === 'success'){
        message.success(res.data.metaData.message)
        this.setState({publishVis: false})
      }
    })
  }

  //删除配置
  handleDeleteClick(code) {
    // this.props.deleteAnalyseTask({code: code})
  }
  render() {
    const  { curRecord, columns, dataSource, publishVis, editVis, resourceId, passDataSource, passBaseInfo } = this.state
    return (
      <div>
          <Table
            size="small" loading={this.state.loading}
            dataSource={dataSource}
            columns={columns} bordered rowKey={record => record.taskId} 
            style={{ padding: 20 }}
            pagination={this.state.paginationNo} />

        <Modal title='编辑资源'
         visible={editVis} width={800}
         onOk={this.handleOk} onCancel={this.handleOk} closable={true}
         footer={null}
         >
           <EditResource curNumber={this.state.categoryNumber} onRef={ref=>this.editData=ref} passDataSource={passDataSource}
               metedataTypes={this.state.metedataTypes} type='publishEdit'  />
        </Modal>

        <Modal title='发布确认' visible={publishVis} width={700} onOk={this.handleOk} onCancel={e => this.setState({publishVis: false})} closable={true}
           bodyStyle={{backgroundColor:'#f0f3f6',overflow:'auto',height:540}} footer={null}
        >
          <div>
            <CommonView resourceId={ resourceId} dataSource={passDataSource} infos={passBaseInfo} />
            <div style={{padding: '0px 20px 0px 20px',  backgroundColor: '#fff',   margin: '24px 16px 0 16px '}}>
              <div style={{ width:'300px', margin:'auto'}}>
                <Button type="primary"  size='small' ghost onClick={e => this.setState({publishVis: false})}>取消</Button>
                <Button type="primary" style={{marginLeft: '30px'}} onClick={this.handlePublishCate} size='small' >发布</Button>
              </div>
            </div>
          </div>
        </Modal>
      </div>
    )
  }
}



const mapState = (state) => {
  // const { taskList} = state.DataAnalyseReducer;
  // const obj = {taskList}
  // console.log('质量剖析',obj)
  // return {
  //    taskList
  // }
}

const mapDispatch = (dispatch) => {
  return {
    setFormInitialData(data) {
      console.log('data', data)
      dispatch(actionCreators.setFormInitialData(data))
    },
    
  }
} 
export default connect(mapState, mapDispatch)(UnAudit)


